#include "GemServer.h"
#include "Conversion.h"

#define GEM_WIDTH 12.0

/*Surcharges operateur*/
sf::Packet& operator <<(sf::Packet& packet, const GemServer& gem) {
	sf::Vector2f position = box2dToSfmlPosition(gem.position);
	
	return packet << position.x << position.y
		<< gem.colour << gem.points << gem.picked;
}

GemServer::GemServer(GemColour gcolour, float posX, float posY, int i) {
	position = sfmlToBox2dPosition(sf::Vector2f(posX + (GEM_WIDTH/2), 
		posY + (GEM_WIDTH/2)));

	id = i;

	width = sfmlToBox2dLength(GEM_WIDTH/2);

	colour = gcolour;
	switch (colour) {
	case YELLOW :
		points = 10;
		break;

	case BLUE :
		points = 20;
		break;

	case GREEN :
		points = 40;
		break;

	case GREY :
		points = 80;
		break;

	case RED :
		points = 100;
		break;

	case PURPLE :
		points = 150;
		break;

	default :
		break;
	}

	picked = false;

	m_contacting = false;
	type = GEM;
}


void GemServer::initialiseB2D(b2World* world) {
	bodyDef.type = b2_staticBody;
	
	box.SetAsBox(width, width);

	bodyDef.position.Set(position.x, position.y);

	body = world->CreateBody(&bodyDef);
	
	fixtureDef.shape = &(box);
	fixtureDef.density = 1.0f; 
	fixtureDef.friction = 0.3f;
	fixtureDef.isSensor = true;

	body->CreateFixture(&fixtureDef);
	body->SetUserData(this);
}

void GemServer::send(sf::TcpSocket& socket) {
	sf::Packet packet;
	packet << *this;
	socket.send(packet);
}

void GemServer::disappear() {
	picked = true;

	//body->SetActive(false);
}

void GemServer::startContact() {
	m_contacting = true;
}

void GemServer::endContact() {
	m_contacting = false;
}